# -*- coding: utf-8 -*-

import mod.client.extraClientApi as clientApi
from gemstoneScripts.config.sys import *

ClientSystem = clientApi.GetClientSystemCls()
comp = clientApi.GetEngineCompFactory()
NativeScreenManager = clientApi.GetNativeScreenManagerCls()
NativeScreenDataType = clientApi.GetMinecraftEnum().UICategory


class Listener(ClientSystem):
    def __init__(self, namespace, system_name):
        super(ClientSystem, self).__init__(namespace, system_name)
        self.screen = None
        self.ListenEvent()
        pass

    def Destroy(self):
        self.UnListenEvent()
        pass

    def ListenEvent(self):
        self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'UiInitFinished', self,
                            self.register_ui)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', GET_PLAYER_ARMOR_EVENT, self,
                            self.get_player_armor)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', OPEN_ICE_ARMOR_UI_EVNET, self,
                            self.open_ui)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', CLOSE_ICE_ARMOR_UI_EVENT, self,
                            self.close_ui)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', SET_ICE_ARMOR_EVENT, self,
                            self.set_ice_armor)
        pass

    def UnListenEvent(self):
        self.UnListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'UiInitFinished', self,
                              self.register_ui)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', GET_PLAYER_ARMOR_EVENT, self,
                              self.get_player_armor)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', OPEN_ICE_ARMOR_UI_EVNET, self,
                              self.open_ui)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', CLOSE_ICE_ARMOR_UI_EVENT, self,
                              self.close_ui)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', SET_ICE_ARMOR_EVENT, self,
                              self.set_ice_armor)
        pass

    def set_ice_armor(self, args):
        if self.screen:
            armor = args['armor']
            self.screen.set_ice_armor(armor)
        pass

    def open_ui(self, args):
        if not self.screen:
            self.screen = clientApi.CreateUI(MOD_NAME, ICE_ARMOR_UI, {'isHud': 1})
        pass

    def close_ui(self, args):
        self.screen.SetRemove()
        self.screen = None
        pass

    def register_ui(self, args):
        clientApi.RegisterUI(MOD_NAME, ICE_ARMOR_UI, ICE_ARMOR_UI_CLS_PATH, 'ice_armor.main')

    def get_player_armor(self, args):
        comp_game = comp.CreateGame(clientApi.GetLevelId())
        player_id = args['playerId']
        armor = comp_game.GetArmorValue(player_id)
        self.NotifyToServer(SET_PLAYER_ARMOR_EVENT, {'armor': armor})
        pass
